using System;
using System.IO;
using System.Xml.Serialization;

namespace SoftwareMonkeys.Scriptech.Projects
{
	public class ProjectAssemblySaver
	{
		public static void Save(string storeName, string groupName, string projectName, ProjectAssembly assembly)
		{
			SaveAssembly(storeName, groupName, projectName, assembly);
		}
		
		public static string SaveAssembly(string storeName, string groupName, string projectName, ProjectAssembly assembly)
		{			
			string assemblyPath = ProjectAssemblyFileNamer.CreateAssemblyPath(storeName, groupName, projectName, assembly);
			
			if (!Directory.Exists(assemblyPath))
				Directory.CreateDirectory(assemblyPath);
			
			string assemblyPropertiesPath = ProjectAssemblyFileNamer.CreateAssemblyPropertiesPath(storeName, groupName, projectName, assembly);
						
			SaveAssemblyToFile(assemblyPropertiesPath, assembly);
			
			return assemblyPropertiesPath;
		}
		

		public static void SaveAssemblyToFile (string filePath, ProjectAssembly assembly)
		{
			using (StreamWriter writer = File.CreateText (filePath)) {
				XmlSerializer serializer = new XmlSerializer (assembly.GetType());
				serializer.Serialize (writer, assembly);
				
			}
		}
	}
}

